﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Common;
using Hangjing.Model;
using Hangjing.SQLServerDAL;

public partial class Admin_Sale_webStatisticsYear : System.Web.UI.Page
{
    Custorder dal = new Custorder();
    Points daltogo = new Points();

    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    /// <summary>
    /// 1表示标签可点
    /// </summary>
    private string flag
    {
        get
        {
            object o = ViewState["flag"];
            return (o == null) ? "1" : Convert.ToString(o);
        }
        set
        {
            ViewState["flag"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SqlWhere = " 1= 1 and orderstatus = 3 and delflag = 0 ";
            SqlWhere = new RightsTool().getOrderWhere(SqlWhere);
            if (Request["tid"] != null)
            {
                int tid = HjNetHelper.GetQueryInt("tid", 0);
                SqlWhere += " and togoid =  " + tid;
                PointsInfo tmdel = daltogo.GetModel(tid);
                h4title.InnerHtml = tmdel.Name + "订单统计";
            }
            WebUtility.SetDDLCity(DDLArea);
            DateTime now = DateTime.Now;
            WebUtility.SelectValue(ddlyear, now.Year.ToString());
        }
    }


    protected void getdays(object sender, RepeaterCommandEventArgs e)
    {
        string str = e.CommandArgument.ToString();
        if (str.IndexOf('-') < 0 || flag == "0")//天数不能再点击
        {
            int tid = 0;
            string date = ddlyear.SelectedValue + "-" + str;
            if (Request["tid"] != null)
            {
                tid = HjNetHelper.GetQueryInt("tid", 0);
            }
            AlertScript.RegScript(Page, UpdatePanel1, "window.open('OrderList.aspx?dates=" + date + "&tid=" + tid + "');");
            return;
        }
        DateTime thedate = Convert.ToDateTime(str + "-1");
        //月份内的天数
        IList<OrderCountInfo> list = new List<OrderCountInfo>();
        IList<OrderCountInfo> rslist = new List<OrderCountInfo>();
        decimal allprice = 0;
        int allcount = 0;
        lbnotice.Style["display"] = "none";
        lbmytype.InnerText = "日期";




        list = dal.SumOrderByTimeUnit(SqlWhere, "day");
        int days = DateTime.DaysInMonth(thedate.Year, thedate.Month);

        for (int i = 1; i <= days; i++)
        {
            string key = str + "-";
            if (i <= 9)
            {
                key += "0";
            }
            key += i;


            int x = 0;
            foreach (OrderCountInfo item in list)
            {
                if (item.CountKey == key)
                {
                    x = 1;

                    rslist.Add(item);
                    allprice += item.CountDecimalValue;
                    allcount += item.CountIntValue;
                }
            }
            if (x == 0)//此月没有订单设置为0
            {
                OrderCountInfo addmodel = new OrderCountInfo();
                addmodel.CountIntValue = 0;
                addmodel.CountDecimalValue = 0;
                addmodel.CountKey = str + "-" + i.ToString();
                rslist.Add(addmodel);
            }
        }

        if (allprice > 0)
        {
            foreach (OrderCountInfo item in rslist)
            {
                item.rat = Convert.ToInt32(item.CountSendFee / allprice * 100 * Convert.ToDecimal(0.8));
            }
        }
        lborder.InnerText = allcount + "";
        lbcount.InnerText = allprice + "";
        WebUtility.BindRepeater(rptsum, rslist);
        flag = "0";
    }

    protected void btExport_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator_super(7);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page, UpdatePanel1, "alert('无操作权限','success','true',5);init();");
            return;
        }

        Response.Redirect("SearchshopStatisticsExcelOrder.aspx?type=1");
    }

    public void BindData()
    {
        IList<OrderCountInfo> list = new List<OrderCountInfo>();
        IList<OrderCountInfo> rslist = new List<OrderCountInfo>();
        decimal allprice = 0;
        int allcount = 0;
        if (ddlmounth.SelectedValue != "0")
        {
            lbnotice.Style["display"] = "none";
            lbmytype.InnerText = "日期";
        }
        else
        {
            lbnotice.Style["display"] = "";
            lbmytype.InnerText = "月份";

            SqlWhere += " and orderdatetime > '" + ddlyear.SelectedValue + "-1-1' and orderdatetime < '" + ddlyear.SelectedValue + "-12-31 23:59:59'";

            list = dal.SumOrderByTimeUnit(SqlWhere, "mounth");

            WebUtility.FixdelCookie("ddlyear");
            WebUtility.FixdelCookie("webStatisticsExcleOrder");
            WebUtility.FixsetCookie("ddlyear", ddlyear.SelectedValue, 1);
            WebUtility.FixsetCookie("webStatisticsExcleOrder", SqlWhere, 1);

            for (int i = 1; i <= 12; i++)
            {
                string key = ddlyear.SelectedValue + "-";
                if (i <= 9)
                {
                    key += "0";
                }
                key += i;

                int x = 0;
                foreach (OrderCountInfo item in list)
                {
                    if (item.CountKey == key)
                    {
                        x = 1;
                        item.CountKey = key;
                        rslist.Add(item);
                        allprice += item.CountDecimalValue;
                        allcount += item.CountIntValue;
                    }
                }
                if (x == 0)//此月没有订单设置为0
                {
                    OrderCountInfo addmodel = new OrderCountInfo();
                    addmodel.CountIntValue = 0;
                    addmodel.CountDecimalValue = 0;
                    addmodel.CountKey = ddlyear.SelectedValue + "-" + i.ToString();
                    addmodel.CountDrinkPrice = 0;
                    addmodel.CountSendFee = 0;
                    rslist.Add(addmodel);
                }
            }
        }
        if (allprice > 0)
        {
            foreach (OrderCountInfo item in rslist)
            {
                item.rat = Convert.ToInt32(item.CountSendFee / allprice * 100 * Convert.ToDecimal(0.8));
            }
        }
        WebUtility.BindRepeater(rptsum, rslist);
        lborder.InnerText = allcount + "";
        lbcount.InnerText = allprice + "";
        flag = "1";
    }

    public void btSearch_Click(object sender, EventArgs e)
    {
        initwhere();
        BindData();
    }

    /// <summary>
    /// 根据key获取时间参数
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    protected string getTimeParams(object s)
    {
        string key = s.ToString();
        string rs = "";
        if (key.Length >= 8)
        {
            rs = "dates="+key;
        }
        else
        {
            rs = "start=" + key+"-1&end="+ Convert.ToDateTime(key + "-1").AddMonths(1).AddSeconds(-1).ToShortDateString() ;
        }
        return rs;
    }


    protected void initwhere()
    {
        SqlWhere = " 1= 1 and orderstatus = 3 and delflag = 0 ";
        SqlWhere = new RightsTool().getOrderWhere(SqlWhere);

        int tid = HjNetHelper.GetQueryInt("tid", 0);
        if (tid > 0)
        {
            SqlWhere += " and togoid =  " + tid;
        }

        if (this.tb_TogoName.Text.Trim() != "")
        {
            SqlWhere += " and TogoId in (select Unid from Points where Name like '%" + Utils.RegEsc(WebUtility.InputText(this.tb_TogoName.Text.Trim())) + "%')";
        }
        if (this.tbPHead.Text.Trim() != "")
        {
            SqlWhere += " AND InUse LIKE '%" + WebUtility.InputText(this.tbPHead.Text.Trim()) + "%' ";
        }
        if (this.tbdelivername.Text.Trim() != "")
        {
            SqlWhere = SqlWhere + " and EXISTS (SELECT * FROM dbo.Deliver WHERE DataId = dbo.Custorder.deliverid AND Name LIKE '%" + WebUtility.InputText(tbdelivername.Text) + "%')";
        }
        if (DDLArea.SelectedValue != "-1")
        {
            SqlWhere += " and cityid = " + DDLArea.SelectedValue + "";
        }
        if (tbgroup.Text != "")
        {
            SqlWhere = SqlWhere + " and EXISTS (SELECT id FROM dbo.DeliverGroup WHERE id = dbo.Custorder.deliverheaderid AND classname LIKE '%" + WebUtility.InputText(tbgroup.Text) + "%') ";
        }
    }
}